package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfAccountType;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoAccountType;
import com.ocom.payconfig.service.XfAccountTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class XfAccountTypeServiceImpl extends ServiceImpl<IDaoAccountType, EntityXfAccountType> implements XfAccountTypeService {


    @Autowired
    private IDaoAccountType accountType;

    // 获取账号类型信息(getXFAccountType)
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityXfAccountType> GetXFAccountType(Long comId ,Integer grouping) {
        QueryWrapper<EntityXfAccountType> query = Wrappers.query();
        query.eq("com_id",comId);
        if(grouping>0){
            query.eq("grouping",grouping);
        }
        query.orderByAsc("acc_type_id");
        return accountType.selectList(query);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    public EntityXfAccountType getXFAccountType(Long comId, Integer accTypeId) {
        QueryWrapper<EntityXfAccountType> query = Wrappers.query();
        query.eq("com_id",comId);
        query.eq("acc_type_id",accTypeId);
        return this.getOne(query);
    }

    // 新增账号类型信息(addXFAccountType)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public Integer AddXFAccountType(Long comId, EntityXfAccountType entityXfAccountType) {
        return accountType.insert(entityXfAccountType);
    }
    // 修改账号类型信息(updateXFAccountType)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public Integer UpdateXFAccountType(Long comId, EntityXfAccountType entityXfAccountType) {
        UpdateWrapper<EntityXfAccountType> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("acc_type_id",entityXfAccountType.getAccTypeId());
        updateWrapper.eq("com_id",entityXfAccountType.getComId());
        return accountType.update(entityXfAccountType,updateWrapper);
    }

    // 获取账号类型明细信息(getXFAccountTypeDetails)
//    @Transactional(isolation = Isolation.READ_COMMITTED)
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public EntityXfAccountType GetXFAccountTypeDetails(Long comId, Integer accTypeId) {
        QueryWrapper<EntityXfAccountType> query = Wrappers.query();
        query.eq("com_id",comId);
        query.eq("acc_type_id",accTypeId);
        return accountType.selectOne(query);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public List<EntityXfAccountType> selectAccTypeName(Long comId, EntityXfAccountType entityXfAccountType) {
        QueryWrapper<EntityXfAccountType> query = Wrappers.query();
        query.eq("com_id",comId);
        query.and(wrapper -> {
            wrapper.or().eq("acc_type_name",entityXfAccountType.getAccTypeName()).or().eq("acc_type_id",entityXfAccountType.getAccTypeId());
        });
        return accountType.selectList(query);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public EntityXfAccountType selectAccTypeNameNull(Long comId, EntityXfAccountType entityXfAccountType) {
        QueryWrapper<EntityXfAccountType> query = Wrappers.query();
        query.eq("com_id",comId);
        query.ne("acc_type_id",entityXfAccountType.getAccTypeId());
        query.eq("acc_type_name",entityXfAccountType.getAccTypeName());
        return accountType.selectOne(query);
    }

    @Override
    public List<EntityXfAccountType> selectXfAccountTypeList(Long comId) {
        QueryWrapper<EntityXfAccountType> query = Wrappers.query();
        query.select("acc_type_id","acc_type_name");
        query.eq("com_id",comId);
        return accountType.selectList(query);
    }
}
